﻿using System;
using System.Collections.Generic;
using BangleCMS.Model;
using BangleCMS.BLL;
using System.Linq;

public partial class System_Main : BasePage
{

    #region 属性
    private List<sys_modular> _modularlist = new List<sys_modular>();
    public List<sys_modular> ModularList { get { return _modularlist; } set { _modularlist = value; } }
    private List<sys_menu> _menulist = new List<sys_menu>();
    public List<sys_menu> MenuList { get { return _menulist; } set { _menulist = value; } }
    #endregion

    #region 加载
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            //设置登录用户
            this.labLoginUser.Text = base.CurrentSession.UserName;
            GetMenuList();
        }
    }
    #endregion

    #region 获得菜单数据
    private void GetMenuList()
    {
        sys_modularBLL ModularBLL = new sys_modularBLL();
        sys_menuBLL MenuBLL = new sys_menuBLL();
        ModularList = ModularBLL.GetModelList("Valid=1").OrderBy(o => o.OrderNo).ToList();
        MenuList = MenuBLL.GetModelList("Valid=1").OrderBy(o => o.OrderNo).ToList();

        if (base.CurrentSession.LoginName != "SystemManager")
        {
            //非系统管理员，隐藏没有权限的目录、菜单

            IDictionary<string, string> UserMenu = base.CurrentSession.UserMenu;
            //去除多余的菜单
            for (int i = MenuList.Count - 1; i >= 0; i--)
            {
                if (!UserMenu.ContainsKey(MenuList[i].MenuCode))
                {
                    //没有权限，则隐藏菜单
                    MenuList.RemoveAt(i);
                }
            }

            //去除多余目录
            for (int i = ModularList.Count - 1; i >= 0; i--)
            {
                if (MenuList.FindAll(delegate(sys_menu p) { return p.ModularId == ModularList[i].ModularId; }).Count <= 0)
                {
                    //没有子菜单，则隐藏目录
                    ModularList.RemoveAt(i);
                }
            }
        }
    }
    #endregion

    #region 退出登录
    protected void lbtnLogout_Click(object sender, EventArgs e)
    {
        base.CurrentSession = null;
        Response.Redirect("~/System/Login/Login.aspx");
    }
    #endregion
}
